package cn.gpdi.framework.dao;

/**
 * Created by 司徒鑫(QQ:156845600) on 2017/5/22.
 */

import cn.gpdi.framework.iface.IDAO;
import cn.gpdi.framework.util.CommUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("commDAO")
public class CommDAO extends SqlSessionDaoSupport implements IDAO {

    @Autowired
    private SqlSession session;


    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public Connection getConnection() throws Exception {

        Connection conn = null;
        conn = session.getConfiguration().getEnvironment().getDataSource().getConnection();
        logger.info("===This Connection isClosed ? " + conn.isClosed());
        return conn;
    }

    @Override
    public Long count(String paramString, Map paramMap) throws Exception {
        return (Long) this.session.selectOne(paramString, paramMap);
    }

    @Override
    public Object selectOneType(String statement, Map paramsMap) throws Exception {
        return  this.session.selectOne(statement, paramsMap);
    }

    @Override
    public Object selectOne(
            String paramString, Object paramMap) throws Exception {
        return this.session.selectOne(paramString, paramMap);
    }

    @Override
    public List<Map<String, Object>> selectList(String paramString, Map paramMap) throws Exception {
        return super.getSqlSession().selectList(paramString, paramMap);
    }

    @Override
    public String sequence(String paramString) throws Exception {
        return null;
    }

    @Override
    public int delete(String paramString, Map paramMap) throws Exception {
        return session.delete(paramString, paramMap);
    }

    @Override
    public int insert(String paramString, Map paramMap) throws Exception {
        return this.session.insert(paramString, paramMap);
    }
    public int insert(String paramString, List paramMap) throws Exception {
        return this.session.insert(paramString, paramMap);
    }


    @Override
    public List<Map<String, Object>> select(String paramString, Map paramMap) throws Exception {
        return session.selectList(paramString, paramMap);
    }


    @Override
    public int update(String paramString, Map paramMap) throws Exception {
        return session.update(paramString, paramMap);
    }

    @Override
    public List<Map<String, Object>> querySQL(String sql) throws Exception {
        if (CommUtil.isEmpty(sql)) {
            return null;
        }
        Map m = new HashMap();
        m.put("SQL", sql);

        List<Map<String, Object>> rs = this.session.selectList("FW_CONFIG.QUERYLIST_Q", m);
        return rs;
    }




    public void setSession(SqlSessionTemplate session) {
        this.session = session;
    }

}